﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;


namespace MsDAL
{
    public partial class Ord3a00DAL : Ord3a00Base
    {
        /// <summary>
        /// 审核
        /// </summary>
        public SqlApp Audit(string listid, string storageid)
        {
            string sql = "exec [dbo].[sp_ord3a00_calctax] @listid, @storageid ";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar),
                new SqlParameter("@storageid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;
            parameters[1].Value = storageid;

            return new SqlApp(sql, new ArrayList(parameters), false, "");
        }

        /// <summary>
        /// 发货
        /// <param name="listid">订单标识主键</param>
        /// <param name="pqty">配送数量</param>
        /// <param name="preqty">赠品数量</param>
        /// </summary>
        public SqlApp Send(string listid, double pqty, double preqty)
        {
            string sql = string.Format("ordsend_sp '{0}','{1}','{2}' ",
                    listid, pqty, preqty);

            return new SqlApp(sql, null, false, "");
        }

        /// <summary>
        /// 更新订单状态为已配货
        /// </summary>
        public SqlApp SendOrder(string listid)
        {
            string sql = "UPDATE Ord3a00 SET status = '3', sendamt = (SELECT SUM(pqty * ordprice) FROM Ord3a01 "
                +"                         WHERE plistid =Ord3a00.listid) "
                +" WHERE listid = @listid AND status IN( '1','11') ";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 发货失败！");
        }

        /// <summary>
        /// 更新订单状态为已打印配货单
        /// </summary>
        public SqlApp AlreadyPrintPreDoc(string listid)
        {
            string sql = "UPDATE Ord3a00 SET status = '11' WHERE listid = @listid AND status = '1' ";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 更新配货单状态失败！");
        }

        /// <summary>
        /// 更新订单状态为已发货
        /// </summary>
        public SqlApp AlreadyPrint(string listid)
        {
            string sql = "UPDATE Ord3a00 SET status = '2' WHERE listid = @listid AND status = '3' ";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 更新订单状态失败！");
        }

        /// <summary>
        /// 更新订单状态为已发货
        /// </summary>
        public SqlApp UpdateSendStatus(string listid)
        {
            string sql = "UPDATE Ord3a00 SET status = '4' WHERE listid = @listid AND status IN ('1','3','11') ";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 更新订单状态失败！");
        }

        /// <summary>
        /// 更新订单状态为已发货
        /// </summary>
        public SqlApp AlreadySendNonePrint(string listid)
        {
            string sql = "UPDATE Ord3a00 SET sendamt = (case when sendamt > 0 then sendamt else (select SUM(ordamt) FROM ord3a01 WHERE ord3a01.plistid = ord3a00.listid) end) "
                +" WHERE listid = @listid AND status IN ('1', '3') ";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 更新订发货状态失败！");
        }
        
        /// <summary>
        /// 回单确认,更新明细表，累减在途数
        /// </summary>
        public SqlApp BackConfirm(string listid, double accqty, double accpreqty, double accamt)
        {
            string sql = @"UPDATE Ord3a01 SET accqty = @accqty, accpreqty =@accpreqty, accamt=@accamt WHERE listid = @listid;
                    UPDATE Pot3a00 SET wayqty = wayqty - @accqty - @accpreqty 
                    WHERE listid IN (SELECT pdlistid FROM Ord3a01 WHERE listid =@listid);
                    ";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar),
                new SqlParameter("@accqty", SqlDbType.Decimal),
                new SqlParameter("@accpreqty", SqlDbType.Decimal),
                new SqlParameter("@accamt", SqlDbType.Decimal)
				};
            parameters[0].Value = listid;
            parameters[1].Value = accqty;
            parameters[2].Value = accpreqty;
            parameters[3].Value = accamt;

            return new SqlApp(sql, new ArrayList(parameters), false, " 回写订单数据失败，回单录入失败！");
        }

        /// <summary>
        /// 更新订单状态为回单已确认
        /// </summary>
        public SqlApp AlreadyBack(string listid, string backno, string remark, double accamt)
        {
            string sql = "UPDATE Ord3a00 SET status = '5',backno =@backno, accremark=@remark, accamt=@accamt WHERE listid = @listid";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar),
                new SqlParameter("@backno", SqlDbType.NVarChar),
                new SqlParameter("@remark", SqlDbType.NVarChar),
                new SqlParameter("@accamt", SqlDbType.Decimal),
				};
            parameters[0].Value = listid;
            parameters[1].Value = backno;
            parameters[2].Value = remark;
            parameters[3].Value = accamt;

            return new SqlApp(sql, new ArrayList(parameters), false, " 更新订单状态失败！");
        }

        /// <summary>
        /// 将状态更新成已经开票
        /// </summary>
        /// <param name="listid"></param>
        /// <returns></returns>
        public SqlApp AlreadyTackTicket(string listid)
        {
            string sql = "UPDATE Ord3a00 SET status = '7' WHERE listid = @listid AND status IN ('5','10')";
            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, " 订单可能已经开票，开票失败！");
        }

        public SqlApp DoAdjust(string id, double adjQty, double adjAmt)
        {
            var sql = "update ord3a00 set adjtotalqty = isnull(adjtotalqty,0) + @adjtotalqty, adjtotalamt=isnull(adjtotalamt, 0) + @adjtotalamt where listid =@listid";

            SqlParameter[] parameters = {
				new SqlParameter("@adjtotalqty", SqlDbType.Decimal),
				new SqlParameter("@adjtotalamt", SqlDbType.Decimal),
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = adjQty;
            parameters[1].Value = adjAmt;
            parameters[2].Value = id;

            return new SqlApp(sql, new ArrayList(parameters), false, "数据保存失败！");
        }

        public SqlApp UpdateSendAmt(string listid)
        {
            var sql = "update ord3a00 set sendamt = (select sum(a.sendamt) from ord3a01 a WHERE a.plistid = dbo.Ord3a00.listid), "
                     + " sendate = (case when sendate is null then getdate() else sendate end) " 
                     +" where listid =@listid";

            SqlParameter[] parameters = {
				new SqlParameter("@listid", SqlDbType.NVarChar)
				};
            parameters[0].Value = listid;

            return new SqlApp(sql, new ArrayList(parameters), false, "数据保存失败！");
        }
    }
}

